package com.xinchuang.controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.xinchuang.utils.CheckUtil;
import com.xinchuang.comment.R;
import com.xinchuang.comment.ResultCode;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;

import com.xinchuang.utils.DateUtils;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.lang3.StringUtils;
import com.xinchuang.service.ComparisonService;

/**
 * ComparisonController
 *
 * @date 2023-08-25
 */
@RestController
@RequestMapping("/comparison")
@Api(value = "MB对比训练", tags = "MB对比训练")
public class ComparisonController {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private ComparisonService comparisonService;

    /**
     * 交卷接口  函数名称dataSave  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/dataSave")
    @ApiOperation("交卷")
    public Result dataSave(@RequestBody JSONObject jsonObject) {
        return comparisonService.dataSave(jsonObject);
    }

    /**
     * 数据获取接口  函数名称comparisonData  功能描述:初始化页面获取题库总数
     *
     * @return 返回参数{}
     */
    @PostMapping("/comparisonData")
    @ApiOperation("数据获取")
    public R comparisonData() {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        String result = "";
        if (result.length() > 0) {
            return R.fail("入参：" + result);
        } else {
            // 调用业务层代码
            String r = comparisonService.comparisonData();
            Set<String> aSet = new HashSet<>();
            JSONObject o = JSON.parseObject(r);
            if (o == null) {
                o = new JSONObject();
            }
            result = jsonLoop(o.get("data"), "", methodName + "AC", aSet);
            result = result + comparisonDataACB(aSet);
            if (result.length() > 0) {
                return R.fail("出参：" + result);
            }
            return JSON.parseObject(r, R.class);
        }
    }

    /**
     * 下一题接口  函数名称nextData  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/nextData")
    @ApiOperation("下一题")
    public Result nextData(@RequestBody JSONObject jsonObject) {
        return comparisonService.nextData(jsonObject);
    }

    /**
     * 确定接口  函数名称savaData  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/savaData")
    @ApiOperation("确定")
    public R savaData(@RequestBody JSONObject jsonObject) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        Set<String> sSet = new HashSet<>();
        String result = jsonLoop(jsonObject, "", methodName + "SC", sSet);
        result = result + savaDataACS(sSet);
        if (result.length() > 0) {
            return R.fail("入参：" + result);
        } else {
            // 调用业务层代码
            String r = comparisonService.savaData(jsonObject);
            Set<String> aSet = new HashSet<>();
            JSONObject o = JSON.parseObject(r);
            if (o == null) {
                o = new JSONObject();
            }
            result = jsonLoop(o.get("data"), "", methodName + "AC", aSet);
            result = result + savaDataACB(aSet);
            if (result.length() > 0) {
                return R.fail("出参：" + result);
            }
            return JSON.parseObject(r, R.class);
        }
    }

    /**
     * 开始答题接口  函数名称startTest  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/startTest")
    @ApiOperation("开始答题")
    public Result startTest(@RequestBody JSONObject jsonObject, @RequestHeader("Id") String id) {
        return comparisonService.startTest(jsonObject, id);
    }


    public static String jsonLoop(Object object, String result, String methodName, Set<String> set) {
        Object v = null;
        if (object instanceof JSONArray || object instanceof ArrayList) {
            JSONArray jsonArray = new JSONArray();
            if (object instanceof ArrayList) {
                jsonArray = JSONArray.parseArray(JSON.toJSONString(object));
            } else {
                jsonArray = (JSONArray) object;
            }
            for (int i = 0; i < jsonArray.size(); i++) {
                result = jsonLoop(jsonArray.get(i), result, methodName, set);
                if (StringUtils.isNotEmpty(result)) {
                    return result;
                }
            }
        }
        if (object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                Object o = entry.getValue();
                if (o instanceof JSONArray || o instanceof ArrayList) {
                    result += selectCheckMathod(entry.getKey(), entry.getValue(), methodName, set);
                    result = jsonLoop(o, result, methodName, set);
                    if (StringUtils.isNotEmpty(result)) {
                        return result;
                    }
                } else if (o instanceof JSONObject) {
                    result = jsonLoop(o, result, methodName, set);
                    if (StringUtils.isNotEmpty(result)) {
                        return result;
                    }
                } else {
//                   FIXME: 未知类型
                }
                v = entry.getValue();
                result += selectCheckMathod(entry.getKey(), v, methodName, set);
            }
        }
        return result;
    }

    private static String selectCheckMathod(String key, Object value, String methodName, Set<String> set) {
        String result = "";
        if ("dataSaveSC".equals(methodName)) {
            result = dataSaveSC(key, value, set);
        }
        if ("dataSaveAC".equals(methodName)) {
            result = dataSaveAC(key, value, set);
        }
        if ("comparisonDataSC".equals(methodName)) {
            result = comparisonDataSC(key, value, set);
        }
        if ("comparisonDataAC".equals(methodName)) {
            result = comparisonDataAC(key, value, set);
        }
        if ("nextDataSC".equals(methodName)) {
            result = nextDataSC(key, value, set);
        }
        if ("nextDataAC".equals(methodName)) {
            result = nextDataAC(key, value, set);
        }
        if ("savaDataSC".equals(methodName)) {
            result = savaDataSC(key, value, set);
        }
        if ("savaDataAC".equals(methodName)) {
            result = savaDataAC(key, value, set);
        }
        if ("startTestSC".equals(methodName)) {
            result = startTestSC(key, value, set);
        }
        if ("startTestAC".equals(methodName)) {
            result = startTestAC(key, value, set);
        }
        return result;
    }


    private static String dataSaveSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("testId".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "试卷标识", isNull, 0, 50);
        }
        if ("answerFlag".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String dataSaveAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("timeTotal".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "耗时时间", isNull, 0, 255);
        }
        if ("numberTotal".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "已答题目数", isNull, 0, 255);
        }
        if ("numberRight".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "答对题目数", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String comparisonDataSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String comparisonDataAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("totalNumber".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "题库总数", isNull, "0", "99999");
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String nextDataSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("type".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "类型", isNull, 0, 50);
        }
        if ("testId".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "试卷标识", isNull, 0, 50);
        }
        if ("answerFlag".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String nextDataAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("imgName1".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "名称1", isNull, 0, 50);
        }
        if ("imgName2".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "名称2", isNull, 0, 50);
        }
        if ("plotList2".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标绘2", isNull, 0, 99999);
        }
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("id".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标识", isNull, 0, 255);
        }
        if ("plotList1".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标绘1", isNull, 0, 99999);
        }
        if ("imgUrl1".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "图1", isNull, 0, 999);
        }
        if ("imgUrl2".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "图2", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String savaDataSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("id".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标识", isNull, 0, 50);
        }
        if ("plotList1".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标绘1", isNull, 0, 99999);
        }
        if ("plotList2".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标绘2", isNull, 0, 99999);
        }
        if ("testId".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "试卷标识", isNull, 0, 50);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String savaDataAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("answerFlag".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String startTestSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("number".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "题号", isNull, 0, 50);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String startTestAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("imgName1".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "名称1", isNull, 0, 50);
        }
        if ("imgName2".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "名称2", isNull, 0, 50);
        }
        if ("plotList2".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标绘2", isNull, 0, 99999);
        }
        if ("id".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标识", isNull, 0, 255);
        }
        if ("plotList1".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标绘1", isNull, 0, 99999);
        }
        if ("imgUrl1".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "图1", isNull, 0, 999);
        }
        if ("imgUrl2".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "图2", isNull, 0, 255);
        }
        if ("testId".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "试卷标识", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String dataSaveACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        list.add("answerFlag");
        list.add("testId");
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String dataSaveACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String comparisonDataACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String comparisonDataACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String nextDataACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        list.add("testId");
        list.add("type");
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String nextDataACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String savaDataACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        list.add("testId");
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String savaDataACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String startTestACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String startTestACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    /**
     * 获取集合中的不同值
     *
     * @param list
     * @param set
     * @return
     */
    private static String getCollectionDifferentValue(List<String> list, Set<String> set, String flag) {
        String result = "";
        for (String str : list) {
            if (!set.contains(str)) {
                result += str + ",";
            }
        }
        if (StringUtils.isNotEmpty(result)) {
            result = StringUtils.strip(result, ",");
            result = "---" + flag + ":" + result + "为必填参数";
        }
        return result;
    }
}
